package com.weixing.mall.provider.controller.admin;

import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.domain.StockCheck;
import com.weixing.mall.provider.model.dto.StockCheckDto;
import com.weixing.mall.provider.model.query.StockCheckDetailsQuery;
import com.weixing.mall.provider.model.query.StockCheckQuery;
import com.weixing.mall.provider.service.IStockCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Author wx
 * @Date 2019/10/23 9:38
 * @Version 1.0
 */
@RestController
@RequestMapping("stockCheck")
@Api(value = "盘点单管理Api",tags = "盘点单管理")
public class StockCheckController extends BaseController {
    @Autowired
    private IStockCheckService iStockCheckService;

    @GetMapping("")
    @ApiOperation(value = "列表查询")
    public Result list(StockCheckQuery stockCheckQuery){
        try{
            return ResultUtil.success(iStockCheckService.selectList(initMpPage(),stockCheckQuery));
        }catch (Exception e){
            return ResultUtil.error("查询出错");
        }

    }
    @GetMapping("total")
    @ApiOperation(value = "列表查询")
    @NoNeedAccess
    public Result total(StockCheckDetailsQuery stockCheckDetailsQuery){
        try{
            return ResultUtil.success(iStockCheckService.selectTotal(stockCheckDetailsQuery));
        }catch (Exception e){
            return ResultUtil.error("查询出错");
        }

    }
    @PostMapping("save")
    @ApiOperation(value = "新增/修改")
    public Result save(@SysInfo SysAuth auth, StockCheckDto stockCheckDto){
        iStockCheckService.save(stockCheckDto,auth.getUid());
        return ResultUtil.success();
    }
    @GetMapping("finish/{stockCheckDtoId}")
    @ApiOperation(value = "完成盘点-最初版")
    public Result finish(@SysInfo SysAuth auth,@PathVariable  Long stockCheckDtoId){
        iStockCheckService.finish(stockCheckDtoId,auth.getUid());
        return ResultUtil.success();
    }

    @GetMapping("mergeStockCheck/{stockCheckDtoId}")
    @ApiOperation(value = "盘点数量合并-拆分版")
    public Result mergeStockCheck(@SysInfo SysAuth auth,@PathVariable  Long stockCheckDtoId){
        iStockCheckService.mergeStockCheck(stockCheckDtoId,auth.getUid());
        return ResultUtil.success();
    }

    @GetMapping("finishDepot/{stockCheckDtoId}")
    @ApiOperation(value = "完成出入库-拆分版")
    public Result finishDepot(@SysInfo SysAuth auth,@PathVariable  Long stockCheckDtoId){
        iStockCheckService.finishDepot(stockCheckDtoId,auth.getUid());
        return ResultUtil.success();
    }

    @GetMapping("publish/{id}")
    @ApiOperation(value = "发布盘点")
    public Result publish(@PathVariable  Long id){
        StockCheck stockCheck=new StockCheck();
        stockCheck.setId(id);
        stockCheck.setIsPublish(WhetherEnum.YES.getValue());
        iStockCheckService.updateById(stockCheck);
        return ResultUtil.success();
    }
    @PostMapping("delAll/{ids}")
    @ApiOperation(value = "批量删除")
    public Result delAll(@PathVariable int [] ids){
        iStockCheckService.removeByIds(Lists.newArrayList(ids));
        return ResultUtil.success();
    }

    @PostMapping("delstockCheck/{id}")
    @ApiOperation(value = "删除")
    public Result delAll(@PathVariable Long id){
        iStockCheckService.removeById(id);
        return ResultUtil.success();
    }

    @GetMapping("selectDetail")
    @ApiOperation(value = "盘点单详情")
    public Result selectDetails(StockCheckDetailsQuery stockCheckDetailsQuery){
        return ResultUtil.success(iStockCheckService.selectDetails(initMpPage(),stockCheckDetailsQuery));
    }

    @PostMapping("selectSpecificDetails")
    @ApiOperation(value = "盘点单具体详情")
    public Result selectSpecificDetails(Long checkId,Long stockPerson){
        return ResultUtil.success(iStockCheckService.selectSpecificDetails(initMpPage(),checkId,stockPerson));
    }
}
